﻿package mortal.game.view.levelReward.panel
{
    import __AS3__.vec.*;
    import com.gengine.resource.*;
    import com.mui.controls.*;
    import fl.data.*;
    import flash.events.*;
    import mortal.game.cache.*;
    import mortal.game.resource.*;
    import mortal.game.view.common.*;
    import mortal.game.view.levelReward.cellrender.*;
    import mortal.game.view.levelReward.other.*;
    import mortal.game.view.signUp.view.*;

    public class LevelRewardVipUpGiftPanel extends SignUpViewBase
    {
        private var _bagGiftList:GTileList;
        private var _levelGiftList:GTileList;
        private var _turnLeftBtn:GLoadedButton;
        private var _turnRightBtn:GLoadedButton;
        private var _selectedGiftIndex:int;
        private var _isCreateChildren:Boolean;
        private var _currSelIndex:int;

        public function LevelRewardVipUpGiftPanel()
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            LoaderHelp.addResCallBack(ResFileConst.levelReward, this.onResLoadCompl);
            return;
        }// end function

        private function onResLoadCompl() : void
        {
            if (isDisposed)
            {
                return;
            }
            this.createChildren();
            return;
        }// end function

        private function createChildren() : void
        {
            this._isCreateChildren = true;
            this.pushUIToDisposeVec(UIFactory.bg(203 - 203, 66 - 66, 691, 545 + 2, this));
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.levelRewardBg, 2, 0, this));
            this._turnLeftBtn = UIFactory.gLoadedButton(ImagesConst.MountLeft_upSkin, 2, 182 - 10, 33, 37, this);
            this._turnLeftBtn.configEventListener(MouseEvent.CLICK, this.onTurnPageHandler);
            this._turnRightBtn = UIFactory.gLoadedButton(ImagesConst.MountRight_upSkin, 485 - 2, 182 - 10, 33, 37, this);
            this._turnRightBtn.configEventListener(MouseEvent.CLICK, this.onTurnPageHandler);
            this._bagGiftList = UIFactory.tileList(26 - 11 + 20, 216 - 70, 475, 120, this);
            this._bagGiftList.columnWidth = 45;
            this._bagGiftList.rowHeight = 60;
            this._bagGiftList.horizontalGap = 0;
            this._bagGiftList.verticalGap = 0;
            this._bagGiftList.setStyle("cellRenderer", LevelRewardVipUpBagGiftCellRender);
            this._levelGiftList = UIFactory.tileList(0, 287 - 72, 670 + 20, 332, this);
            this._levelGiftList.rowHeight = 87 - 4;
            this._levelGiftList.columnWidth = 662 + 12;
            this._levelGiftList.horizontalGap = 0;
            this._levelGiftList.verticalGap = 0;
            this._levelGiftList.setStyle("cellRenderer", LevelRewardVipUpAllLevelsGiftCellRender);
            this.initGiftList();
            this.initBtnState();
            this.initLevelGiftList();
            return;
        }// end function

        private function initGiftList() : void
        {
            this._bagGiftList.dataProvider = this.getBagGiftListDataProvider();
            this._bagGiftList.drawNow();
            return;
        }// end function

        private function initBtnState() : void
        {
            if (this._currSelIndex == 0)
            {
                this._turnLeftBtn.filterEnabled = false;
            }
            else
            {
                this._turnLeftBtn.filterEnabled = true;
            }
            var _loc_1:* = Cache.instance.levelReward.giftBags;
            if (this._currSelIndex + 9 >= (_loc_1.length - 1))
            {
                this._turnRightBtn.filterEnabled = false;
            }
            else
            {
                this._turnRightBtn.filterEnabled = true;
            }
            return;
        }// end function

        private function initLevelGiftList() : void
        {
            this._levelGiftList.dataProvider = this.getLevelGiftListDataProvider();
            this._levelGiftList.drawNow();
            this._levelGiftList.verticalScrollBar.x = 675;
            this._levelGiftList.scrollToIndex(this._selectedGiftIndex);
            return;
        }// end function

        public function refresh(param1:Boolean = false) : void
        {
            if (this._isCreateChildren)
            {
                this._selectedGiftIndex = -1;
                this._bagGiftList.dataProvider = this.getBagGiftListDataProvider();
                this._bagGiftList.drawNow();
                this._levelGiftList.dataProvider = this.getLevelGiftListDataProvider();
                this._levelGiftList.drawNow();
                this._levelGiftList.verticalScrollBar.x = 675;
                this._levelGiftList.scrollToIndex(this._selectedGiftIndex);
            }
            return;
        }// end function

        private function getBagGiftListDataProvider() : DataProvider
        {
            var _loc_5:Vector.<LevelRewardGiftBag> = null;
            var _loc_1:* = new DataProvider();
            var _loc_2:* = Cache.instance.levelReward.giftBags;
            var _loc_3:* = this._currSelIndex;
            var _loc_4:* = this._currSelIndex + 10;
            if (_loc_4 >= _loc_2.length)
            {
                _loc_5 = _loc_2.slice(_loc_3);
            }
            else
            {
                _loc_5 = _loc_2.slice(_loc_3, _loc_4);
            }
            var _loc_6:int = 0;
            while (_loc_6 < _loc_5.length)
            {
                
                _loc_1.addItem(_loc_5[_loc_6]);
                _loc_6++;
            }
            return _loc_1;
        }// end function

        private function getLevelGiftListDataProvider() : DataProvider
        {
            var _loc_1:* = new DataProvider();
            var _loc_2:* = Cache.instance.levelReward.giftGroups;
            var _loc_3:* = _loc_2.length;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            while (_loc_5 < _loc_3)
            {
                
                _loc_2[_loc_5].bgType = _loc_4;
                if (++_loc_4 > 2)
                {
                    ++_loc_4 = 0;
                }
                _loc_1.addItem(_loc_2[_loc_5]);
                if (this._selectedGiftIndex <= 0)
                {
                    if (_loc_2[_loc_5].receiveType == LevelRewardGiftDataGroup.CanReceive)
                    {
                        this._selectedGiftIndex = _loc_5;
                    }
                }
                _loc_5++;
            }
            return _loc_1;
        }// end function

        private function onTurnPageHandler(event:MouseEvent) : void
        {
            if (event.target == this._turnLeftBtn)
            {
                var _loc_2:String = this;
                var _loc_3:* = this._currSelIndex - 1;
                _loc_2._currSelIndex = _loc_3;
            }
            if (event.target == this._turnRightBtn)
            {
                var _loc_2:String = this;
                var _loc_3:* = this._currSelIndex + 1;
                _loc_2._currSelIndex = _loc_3;
            }
            this.initGiftList();
            this.initBtnState();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            if (!this._isCreateChildren)
            {
                return;
            }
            this._isCreateChildren = false;
            this._bagGiftList.dispose(param1);
            this._levelGiftList.dispose(param1);
            this._turnLeftBtn.dispose();
            this._turnRightBtn.dispose();
            this._bagGiftList = null;
            this._levelGiftList = null;
            this._turnLeftBtn = null;
            this._turnRightBtn = null;
            this._currSelIndex = 0;
            return;
        }// end function

    }
}
